wx test 2.py (4707B)
1 ### WX TEST ### 2 # Copyright David Barker 2010 3 # 4 # A simple test of using wxPython to create a gui for PyIgnition 5 6 7 import wx, sys, os, pygame, PyIgnition 8 9 class PygameDisplay(wx.Window): 10 def __init__(self, parent, ID): 11 wx.Window.__init__(self, parent, ID) 12 self.parent = parent 13 self.hwnd = self.GetHandle() 14 os.environ['SDL_VIDEODRIVER'] = 'windib' 15 os.environ['SDL_WINDOWID'] = str(self.hwnd) 16 pygame.display.init() 17 self.screen = pygame.display.set_mode() 18 self.size = self.GetSizeTuple() 19 self.timer = wx.Timer(self) 20 self.Bind(wx.EVT_PAINT, self.OnPaint) 21 self.Bind(wx.EVT_TIMER, self.Update, self.timer) 22 self.Bind(wx.EVT_SIZE, self.OnSize) 23 self.fps = 60.0 24 self.timespacing = 1000.0 / self.fps 25 self.timer.Start(self.timespacing, False) 26 27 self.effect = PyIgnition.ParticleEffect(self.screen, (0, 0), self.size) 28 self.source = self.effect.CreateSource((self.size[0] / 2, self.size[1]), initspeed = 5.0, initdirection = 0.0, initspeedrandrange = 2.0, initdirectionrandrange = 0.2, particlesperframe = 10, particlelife = 200, drawtype = PyIgnition.DRAWTYPE_CIRCLE, colour = (255, 20, 200), radius = 0.0) 29 self.source.CreateParticleKeyframe(100, radius = 20.0) 30 self.source.CreateParticleKeyframe(25, colour = (255, 0, 100)) 31 self.source.CreateParticleKeyframe(50, colour = (100, 255, 100)) 32 self.source.CreateParticleKeyframe(75, colour = (0, 100, 255)) 33 self.source.CreateParticleKeyframe(200, colour = (0, 0, 0)) 34 self.grav = self.effect.CreateDirectedGravity(0.0, 0.2, [1, 0]) 35 self.circle = self.effect.CreateCircle((300, 300), (255, 255, 255), 0.5, 50) 36 37 def Update(self, event): 38 self.effect.Update() 39 self.Redraw() 40 if self.parent.val % 30 == 0: 41 self.source.ConsolidateKeyframes() 42 self.grav.ConsolidateKeyframes() 43 self.circle.ConsolidateKeyframes() 44 45 def Redraw(self): 46 self.screen.fill((0, 0, 0)) 47 self.effect.Redraw() 48 pygame.display.update() 49 50 def OnPaint(self, event): 51 self.Redraw() 52 53 def OnSize(self, event): 54 self.size = self.GetSizeTuple() 55 newsourcepos = (self.size[0] / 2, self.size[1]) 56 self.source.SetPos(newsourcepos) 57 self.circle.SetPos((newsourcepos[0] - 30, newsourcepos[1] - 200)) 58 59 def Kill(self, event): 60 # Make sure Pygame can't be asked to redraw /before/ quitting 61 # (Otherwise wx seems to call Draw between quitting Pygame and destroying the frame) 62 self.Unbind(event = wx.EVT_PAINT, handler = self.OnPaint) 63 self.Unbind(event = wx.EVT_TIMER, handler = self.Update, source = self.timer) 64 65 class Frame(wx.Frame): 66 def __init__(self, parent): 67 wx.Frame.__init__(self, parent, -1) 68 self.display = PygameDisplay(self, -1) 69 self.statusbar = self.CreateStatusBar() 70 self.statusbar.SetFieldsCount(3) 71 self.statusbar.SetStatusWidths([-3, -4, -2]) 72 self.statusbar.SetStatusText("ExeSoft Obsidian", 0) 73 self.statusbar.SetStatusText("Look, it's a nifty status bar!!!", 1) 74 self.Bind(wx.EVT_SIZE, self.OnSize) 75 self.Bind(wx.EVT_CLOSE, self.Kill) 76 self.val = 0 77 self.SetTitle("ExeSoft Obsidian") 78 self.gravslider = wx.Slider(self, wx.ID_ANY, 0, -50, 50, style = wx.SL_HORIZONTAL | wx.SL_LABELS) 79 self.gravslider.SetTickFreq(0.1, 1) 80 self.timer = wx.Timer(self) 81 self.Bind(wx.EVT_SCROLL, self.OnScroll) 82 self.Bind(wx.EVT_SIZE, self.OnSize) 83 self.Bind(wx.EVT_TIMER, self.Update, self.timer) 84 self.timer.Start((100.0 / self.display.fps)) 85 sizer = wx.BoxSizer(wx.VERTICAL) 86 sizer.Add(self.gravslider, 0, flag = wx.EXPAND) 87 sizer.Add(self.display, 1, flag = wx.EXPAND) 88 self.SetAutoLayout(True) 89 self.SetSizer(sizer) 90 self.Layout() 91 92 def Kill(self, event): 93 self.display.Kill(event) 94 pygame.quit() 95 self.Destroy() 96 97 def OnSize(self, event): 98 self.Layout() 99 100 def Update(self, event): 101 self.val += 1 102 self.statusbar.SetStatusText("Frame %i" % self.val, 2) 103 104 def OnScroll(self, event): 105 self.display.grav.SetStrength(float(self.gravslider.GetValue()) / 100.0) 106 107 class App(wx.App): 108 def OnInit(self): 109 self.frame = Frame(parent = None) 110 self.frame.Show() 111 self.SetTopWindow(self.frame) 112 113 return True 114 115 if __name__ == "__main__": 116 app = App() 117 app.MainLoop()